﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AVCUMPlatform.HelperLibrary
{
    /// <summary>
    /// 公共工具类
    /// </summary>
    public class CommonHelper
    {
        /// <summary>
        /// 批量处理任务工具方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tList"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="action"></param>
        public static void BatchProcess<T>(List<T> tList, int pageSize, Action<List<T>> action)
        {
            if (tList == null || !tList.Any())
            {
                return;
            }

            int pageIndex = 1;
            List<T> tempList = tList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            while (tempList.Any())
            {
                action.Invoke(tempList);
                pageIndex += 1;
                tempList = tList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        /// <summary>
        /// 默认密码
        /// </summary>
        public static readonly string DefaultPassword = MD5EncryptHelper.MD5Encrypt("pw_a1b2c3");

        /// <summary>
        /// 加密盐
        /// </summary>
        public static readonly string EncryptSalt = "_avc_";

        /// <summary>
        /// 电话号码加密
        /// </summary>
        /// <param name="phoneNum"></param>
        /// <returns></returns>
        public static string PhoneEncrypt(string phoneNum)
        {
            if (string.IsNullOrEmpty(phoneNum)
                || phoneNum.Length != 11
                || phoneNum.Contains(EncryptSalt))
            {
                return phoneNum;
            }

            string partStart = phoneNum.Substring(0, 3);
            string partCenter = phoneNum.Substring(3, 4);
            string partEnd = phoneNum.Substring(7, 4);
            return $"{partStart}{EncryptSalt}{DESEncrypt.EncryptDES(partCenter, $"{partStart}{partEnd}&")}{partEnd}";
        }

        /// <summary>
        /// 电话号码解密
        /// </summary>
        /// <param name="encryptedPhoneNum"></param>
        /// <returns></returns>
        public static string PhoneDecrypt(string encryptedPhoneNum)
        {
            if (string.IsNullOrEmpty(encryptedPhoneNum)
                || !encryptedPhoneNum.Contains(EncryptSalt))
            {
                return encryptedPhoneNum;
            }

            string partStart = encryptedPhoneNum.Substring(0, 3);
            string partCenter = encryptedPhoneNum.Substring(3, encryptedPhoneNum.Length - 7);
            partCenter = partCenter.Replace(EncryptSalt, string.Empty);
            string partEnd = encryptedPhoneNum.Substring(encryptedPhoneNum.Length - 4, 4);
            return $"{partStart}{DESEncrypt.DecryptDES(partCenter, $"{partStart}{partEnd}&")}{partEnd}";
        }

        /// <summary>
        /// 电话号码隐藏
        /// </summary>
        /// <param name="telephoneSource"></param>
        /// <returns></returns>
        public static string PhoneHide(string telephoneSource)
        {
            string telephone = string.Empty;
            if (!string.IsNullOrEmpty(telephoneSource)
                && telephoneSource.Contains(EncryptSalt))
            {
                string partStart = telephoneSource.Substring(0, 3);
                string partEnd = telephoneSource.Substring(telephoneSource.Length - 4, 4);
                telephone = $"{partStart}****{partEnd}";
            }
            return telephone;
        }

        /// <summary>
        /// 列表空判断
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool ListIsNullOrEmpty<T>(List<T> list)
        {
            if (list == null || list.Count == 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取学期周
        /// </summary>
        /// <param name="termBeginDate"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static int GetSemesterWeekNum(DateTime termBeginDate, DateTime date)
        {
            DateTime termBeginDateWeekFirstDay = DateTimeHelper.GetWeekDay(termBeginDate, 1);
            DateTime dateWeekFirstDay = DateTimeHelper.GetWeekDay(date, 1);
            int diffDay = (int)DateTimeHelper.DateDiff(termBeginDateWeekFirstDay, dateWeekFirstDay, TimeType.Day);
            return diffDay / 7 + 1;
        }

        /// <summary>
        /// 获取场地订单编号
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetSiteOrderNum(int userId)
        {
            return $"CD{DateTime.Now.ToNum()}{userId}";
        }

        /// <summary>
        /// 获取自习订单编号
        /// </summary>
        /// <returns></returns>
        public static string GetSeatOrderNum()
        {
            return $"ZX{DateTime.Now.ToOrderNum()}";
        }

        /// <summary>
        /// 获取调课场地编号
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetAdjustOrderNum()
        {
            return $"TK{DateTime.Now.ToOrderNum()}";
        }

        /// <summary>

        /// 获取批量场地预约编号
        /// </summary>
        /// <returns></returns>
        public static string GetBatchSiteOrderNum()
        {
            return $"PL{DateTime.Now.ToOrderNum()}";
        }

        /// <summary>
        /// 获取场地预约编号
        /// </summary>
        /// <returns></returns>
        public static string GetSiteOrderNum()
        {
            return $"CD{DateTime.Now.ToOrderNum()}";
        }
        /// 获取停课场地编号
        /// </summary>
        /// <returns></returns>
        public static string GetStopOrderNum()
        {
            return $"SK{DateTime.Now.ToOrderNum()}";
        }
        /// 获取停课场地编号
        /// </summary>
        /// <returns></returns>
        public static string GetLaboratoryOrderNum()
        {
            return $"SY{DateTime.Now.ToOrderNum()}";
        }
        /// 反序列化列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static List<T> DeserializeList<T>(string jsonStr)
        {
            if (string.IsNullOrEmpty(jsonStr))
            {
                return new List<T>();
            }
            return JsonConvert.DeserializeObject<List<T>>(jsonStr);
        }

        /// <summary>
        /// 获取美剧描述
        /// </summary>
        /// <param name="value"></param>
        /// <param name="nameInstead"></param>
        /// <returns></returns>
        public static string GetEnumDescription(Enum value, Boolean nameInstead = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }

            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

            if (attribute == null && nameInstead == true)
            {
                return name;
            }
            return attribute?.Description;
        }

        /// <summary>
        /// id 列表转换为字符串，方括号附近添加 逗号，便于模糊查询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static string ToDBStr(List<int> ids)
        {
            if (ListIsNullOrEmpty(ids))
            {
                return "[]";
            }
            string str = JsonConvert.SerializeObject(ids);
            str = str.Replace("[", "[,").Replace("]", ",]");
            return str;
        }

        /// <summary>
        /// id 列表转换为字符串，方括号附近添加 逗号，便于模糊查询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static string ToDBStr(string ids)
        {
            return ToDBStr(ToIdList(ids));
        }

        /// <summary>
        /// 数据库存的 Id 字符串 转换为列表
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<int> ToIdList(string str)
        {
            if (string.IsNullOrEmpty(str) || str == "[]")
            {
                return new List<int>();
            }
            str = str.Replace("[,", "[").Replace(",]", "]");
            return JsonConvert.DeserializeObject<List<int>>(str);
        }
    }
}
